package com.mrjy.wechat.controller;

import com.mrjy.common.entity.VideoInfo;
import com.mrjy.video.dao.IVideoInfoDao;
import com.mrjy.wechat.bo.IWeChatOAuthBo;
import com.mrjy.wechat.bo.IWechatTemplateBo;
import com.mrjy.wechat.conts.WxConsts;
import com.mrjy.wechat.dto.TemplateMessage;
import com.mrjy.wechat.dto.Video;
import com.mrjy.wechat.process.AccessToken;
import com.mrjy.wechat.util.WeChatUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author Javin
 * @date 2017年7月20日
 * @desc 微信消息模板类
 * @comment
 */
@RequestMapping("weChat/template")
@Controller
public class WechatTemplateController {
	
	@Resource
	private IWechatTemplateBo templateBo;
	
	@Resource
	private IWeChatOAuthBo weChatOAuthBo;

    @Resource
    private IVideoInfoDao videoInfoDao;

    /**
     * @author Javin
	 * @date 2017年7月20日
	 * @desc 发送消息模板
	 * @param message
	 * @return
	 */
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	@ResponseBody
	public Object send(TemplateMessage message){
		
		AccessToken accessToken = WxConsts.getAccessToken();
		if(null == accessToken){
			accessToken = weChatOAuthBo.getAccessToken(WxConsts.APPID, WxConsts.APPSECRET);
		}
		
		return templateBo.send(message,accessToken);
	}
	
	/**
	 * @author Javin
	 * @date 2017年7月20日
	 * @desc 获取所有微信消息模板
	 * @return
	 */
	@RequestMapping(value = "/getAllTemplate",method = RequestMethod.GET)
	@ResponseBody
	public Object getAllTemplate(){
		AccessToken accessToken = WxConsts.getAccessToken();
		if(accessToken == null){
			accessToken = weChatOAuthBo.getAccessToken(WxConsts.APPID, WxConsts.APPSECRET);
		}
		return templateBo.getAllTemplate(accessToken);
	}
	
	@RequestMapping(value = "/getTeaplate", method = RequestMethod.POST)
	public Object getTeaplate(String templateIdShort){
		AccessToken accessToken = WxConsts.getAccessToken();
		if(accessToken == null){
			accessToken = weChatOAuthBo.getAccessToken(WxConsts.APPID, WxConsts.APPSECRET);
		}
		return templateBo.getTeaplate(accessToken,templateIdShort);
		
	}


    /**
     * @param message
     * @return
     * @author lyon
     * @date 2018年7月20日
     * @desc 发送消息模板测试用例
     */
    @RequestMapping(value = "/sendNewClassTest")
    @ResponseBody
    public Object sendNewClassTest(Integer videoId, String openId) {
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setId(videoId);
        videoInfo = videoInfoDao.queryVideoInfoById(videoInfo);

        Object release = WeChatUtil.sendWechatTemplateMessageCourseRelease("每天8分钟，解决一个家庭教育问题！", "千节课程等你来，学完有积分哦！点击进入！", videoInfo.getVideoName(), videoInfo.getTypeName()
                , videoInfo.getUserName(), "2018.07.20 05:20",
                WxConsts.PUBLIC_DOMAIN + "/index.html?from=weClasses/course_detail.html@_@vid=" + videoInfo.getId(), openId);

        return release;
    }

    /**
     * @param message
     * @return
     * @author lyon
     * @date 2018年7月20日
     * @desc 发送消息模板测试用例
     */
    @RequestMapping(value = "/sendNewClassTest2")
    @ResponseBody
    public Object sendNewClassTest2(Integer videoId, String[] openIds) {
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setId(videoId);
        videoInfo = videoInfoDao.queryVideoInfoById(videoInfo);


        ArrayList<String> arrayList = new ArrayList<>();

        for (String openId : openIds) {
            arrayList.add(openId);
        }

        Object release = WeChatUtil.sendWechatTemplateMessageCourseRelease2("每天8分钟，解决一个家庭教育问题！", "千节课程等你来，学完有积分哦！点击进入！", videoInfo.getVideoName(), videoInfo.getTypeName()
                , videoInfo.getUserName(), "2018.07.20 05:20",
                WxConsts.PUBLIC_DOMAIN + "/index.html?from=weClasses/course_detail.html@_@vid=" + videoInfo.getId(), arrayList);

        return release;
    }

}
